package com.example.ipcserver.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;

import com.example.ipc.Book;
import com.example.ipc.IBookByAidl;
import com.example.ipc.IBookListener;

import java.util.ArrayList;
import java.util.List;

public class AidlServer extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    private final List<Book> mBooks = new ArrayList<>();

    @Override
    public void onCreate() {
        super.onCreate();
        mBooks.add(new Book("Java,从入门到精通"));
        mBooks.add(new Book("Android,从入门到放弃"));
        mBooks.add(new Book("Android Studio"));
        mBooks.add(new Book("Android,从放弃到突破"));
    }

    Binder binder = new IBookByAidl.Stub() {
        @Override
        public List<Book> getBooks() throws RemoteException {
            return mBooks;
        }

        @Override
        public boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {
            return super.onTransact(code, data, reply, flags);
        }

        @Override
        public void addBook(Book book) throws RemoteException {
            mBooks.add(book);
        }

        @Override
        public void addListener(IBookListener listener) throws RemoteException {

        }
    };

}
